package main

func calculateMinimumHP(dungeon [][]int) int {
	wide := len(dungeon)
	high := len(dungeon[0])
	for i := wide - 1; i >= 0; i-- {
		for j := high - 1; j >= 0; j-- {
			if i == wide-1 && j == high-1 {
				dungeon[i][j] = Min(0, dungeon[i][j])
			} else if i == wide-1 {
				dungeon[i][j] = Min(0, dungeon[i][j]+dungeon[i][j+1])
			} else if j == high-1 {
				dungeon[i][j] = Min(0, dungeon[i][j]+dungeon[i+1][j])
			} else {
				dungeon[i][j] = Max(dungeon[i][j]+dungeon[i][j+1], dungeon[i][j]+dungeon[i+1][j])
				dungeon[i][j] = Min(0, dungeon[i][j])
			}
		}
	}
	return Min(0, dungeon[0][0])*-1 + 1
}

func Max(a int, b int) int {
	if a > b {
		return a
	}
	return b
}

func Min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	calculateMinimumHP([][]int{
		{-2, -3, 3},
		{-5, -10, 1},
		{10, 30, -5}})
}
